/**
 * Title: AppActivity.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-11-24 上午11:32:05
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.activities;

import java.util.ArrayList;
import java.util.List;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.ui.adapters.AppGrideViewAdapter;
import com.luoxudong.phonenumber.ui.adapters.vo.AppItem;
import com.luoxudong.phonenumber.ui.common.listener.OnItemClickAvoidForceListener;
import com.luoxudong.phonenumber.ui.widgets.titlebar.ITitleBarOnClickListener;
import com.luoxudong.phonenumber.ui.widgets.titlebar.TitleBarView;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

/** 
 * ClassName: AppActivity
 * Description:应用tab
 * Create by 罗旭东
 * Date 2013-11-24 上午11:32:05
 */
public class AppActivity extends AbsSubActivity{
	private TitleBarView titleBarView = null;
	private GridView appGridView = null;
	private AppGrideViewAdapter adapter = null;
	private List<AppItem> appItems = null;
	
	@Override
	protected int getContentViewLayoutResId() {
		return R.layout.app_layout;
	}

	@Override
	protected void onCreateFindView(Bundle savedInstanceState) {
		titleBarView = (TitleBarView)findViewById(R.id.title_bar);
		appGridView = (GridView)findViewById(R.id.app_gridview);
		
		titleBarView.setTitle(R.string.app);
	}

	@Override
	protected void onCreateAddListener(Bundle savedInstanceState) {
		titleBarView.setTitleBarOnClickListener(titleBarOnClickListener);
		appGridView.setOnItemClickListener(onItemClickListener);
	}

	@Override
	protected void onCreateInitData(Bundle savedInstanceState) {
		appItems = loadAppItems();
		adapter = new AppGrideViewAdapter(this, appItems);
		appGridView.setAdapter(adapter);
	}

	@Override
	protected void onCreateTaskLoadData() {
		
	}

	@Override
	protected void onCreateTaskAddView() {
		
	}

	@Override
	protected void refreshUI(Object obj) {
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
		{
			exitApp();
			return true;
			
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private List<AppItem> loadAppItems()
	{
		List<AppItem> newAppItems = new ArrayList<AppItem>();
		AppItem appItem = null;
		
		appItem = new AppItem();
		appItem.setAppIconResId(R.drawable.app_phone_charge);
		appItem.setAppName("归属地查询");
		appItem.setTargetCls(AppAttributionAreaActivity.class);
		newAppItems.add(appItem);
		
		appItem = new AppItem();
		appItem.setAppIconResId(R.drawable.app_phone_charge);
		appItem.setAppName("号码价值评估");
		appItem.setTargetCls(AppPhoneAssessActivity.class);
		newAppItems.add(appItem);
		
		appItem = new AppItem();
		appItem.setAppIconResId(R.drawable.app_phone_charge);
		appItem.setAppName("号码吉凶测试");
		appItem.setTargetCls(AppPhoneAuspiciousActivity.class);
		newAppItems.add(appItem);
		return newAppItems;
	}
	
	public void onBackPressed() {
		exitApp();
	};
	
	private OnItemClickAvoidForceListener onItemClickListener = new OnItemClickAvoidForceListener() {
		
		@Override
		public void onItemClickAvoidForce(AdapterView<?> parent, View view, int position, long id) {
			AppItem appItem = adapter.getItem(position);
			
			if (appItem != null)
			{
				Intent intent = new Intent(AppActivity.this, appItem.getTargetCls());
				startActivity(intent);
			}
		}
	};
	
	private ITitleBarOnClickListener titleBarOnClickListener = new ITitleBarOnClickListener() {
		
		@Override
		public void onRightClick(View v) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLeftClick(View v) {
			// TODO Auto-generated method stub
			
		}
	};

}
